<?php

class Admin_FeedbackController extends JControllerAdmin
{
    /**
     * @return void
     */
    public function init()
    {
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        // BEGIN: Tạo đối tượng Paginator
        $dbSelect = JDbAdapter::factory()
            ->select()
            ->from('FEEDBACKS');

        // Tìm kiếm theo tiêu đề feedback (nếu có)
        $keyWords = $this->_request->getParam('keyWords');
        if (isset($keyWords)) {
            $dbSelect->where("Title LIKE '%$keyWords%'");
            $this->view->enteredKeyWords = $keyWords;
        }

        // Lọc các feedback đã hoặc chưa xử lý (nếu có)
        $isProcess = $this->_request->getParam('isProcess');
        if (isset($isProcess)) {
            $dbSelect->where('IsProcess = ?', (bool)$isProcess);
            $this->view->isProcess = (bool)$isProcess;
        }

        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($dbSelect)
        );
        // END: Tạo đối tượng Paginator

        // Get and set current page
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $feedbacks = $paginator->getItemsByPage($page);

        // Asign data for view
        $this->view->feedbacks = $feedbacks;
        $this->view->paginator = $paginator;
    }

    /**
     * @return void
     */
    public function addAction()
    {
    }

    /**
     * @return void
     */
    public function deleteAction()
    {
    }

    /**
     * @return void
     */
    public function updateAction()
    {
    }

    /**
     * @return void
     */
    public function confirmAction()
    {
        /** @var $flashMessenger Zend_Controller_Action_Helper_FlashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/feedback/index');
        }

        $feedbackID = $this->_request->getParam('feedbackID');
        if (!Application_Model_Feedback::confirmFeedback($feedbackID)) {
            $flashMessenger->addMessage('Confirm Fail');
        } else {
            $flashMessenger->addMessage("Confirm $feedbackID Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * @return void
     */
    public function unconfirmAction()
    {
        /** @var $flashMessenger Zend_Controller_Action_Helper_FlashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/feedback/index');
        }

        $feedbackID = $this->_request->getParam('feedbackID');
        if (!Application_Model_Feedback::unconfirmFeedback($feedbackID)) {
            $flashMessenger->addMessage('Unconfirm Fail');
        } else {
            $flashMessenger->addMessage("Unconfirm $feedbackID Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}

